package com.milk.base;

import android.support.annotation.LayoutRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewStub;

import com.milk.flux.actions.BaseLoadDataActionCreator;
import com.milk.flux.stores.BaseLoadDataStore;

import butterknife.ButterKnife;

/**
 * Created by oceanzhang on 16/4/8.
 */
public abstract class BaseLoadWithRefreshFragment<T,S extends BaseLoadDataStore<T>,C extends BaseLoadDataActionCreator<T>> extends BaseLoadDataFragment<T,S,C> implements SwipeRefreshLayout.OnRefreshListener{

    SwipeRefreshLayout mSwipeRefreshLayout;
    ViewStub refreshViewStub;
    View refreshContentView;
    @Override
    protected final @LayoutRes int getContentLayoutId() {
        return R.layout.fragment_base_load_refresh;
    }

    @Override
    public final void initContentView(View view) {
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(
                com.milk.base.R.color.swiperefresh_color1, com.milk.base.R.color.swiperefresh_color2,
                com.milk.base.R.color.swiperefresh_color3, com.milk.base.R.color.swiperefresh_color4);
        refreshViewStub = (ViewStub) view.findViewById(R.id.frag_base_load_refresh_content);
        refreshViewStub.setLayoutResource(getRefreshContentLayoutId());
        refreshContentView = refreshViewStub.inflate();
        initRefreshContentView(refreshContentView);
    }

    protected void initRefreshContentView(View view){
        ButterKnife.bind(this, view);
    }

    protected abstract @LayoutRes int getRefreshContentLayoutId();

    @Override
    public void onRefresh() {
        actionsCreator().loadData(createRequest());
    }

    @Override
    protected void bindView(T t) {
        mSwipeRefreshLayout.setRefreshing(false);
    }
}
